package faceinterface;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public interface WeaponFactory {

    final Map<Class<? extends WeaponFactory>, WeaponFactory> map = new ConcurrentHashMap<>();

    public static WeaponFactory getWeaponFactoryByClass(Class<? extends WeaponFactory> weaponFactoryClass) {
        WeaponFactory factory = map.get(weaponFactoryClass);
        if(factory != null) return factory;
        try {
            factory = weaponFactoryClass.newInstance();
            map.put(weaponFactoryClass, factory);
            return factory;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    final Weapon defaultWeapon = new Gun();

    default Weapon getDefaultWeapon() {
        return defaultWeapon;
    }

    abstract Weapon getWeaponByClass(Class<? extends Weapon> weaponClass);
    
    public static void main(String[] args) {
        System.out.println(Wf.defaultWeapon);
        Weapon weaponByClass = WeaponFactory.getWeaponFactoryByClass(Wf.class).getWeaponByClass(Gun.class);
        weaponByClass.beUse();
    }
    
}
class Wf implements WeaponFactory{
    
    @Override
    public Weapon getWeaponByClass(Class<? extends Weapon> weaponClass) {
        try {
            return weaponClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}